package com.malou.client.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.i18n.client.constants.CurrencyCodeMapConstants;

public class Utilities {
	public static Integer itemsPerPage = 20;
	public static Double igv_value = 1.18;
	public static String tienda = null;
	public static String currencyCode = "PEN";
	private static CurrencyCodeMapConstants currencyCodeMapConstants = GWT.create(CurrencyCodeMapConstants.class);
	
	public static boolean hasValue(TextField<String> field) {
	    return field.getValue() != null && field.getValue().length() > 0;
	}
	public static boolean hasValue(NumberField field) {
	    return field.getValue() != null && field.getValue().toString().length() > 0;
	}
	public static String toIds(List<BeanModel> list) {
		String ids="";
		for(BeanModel item : list){						
			ids += ","+item.get("id");
		 }
		 ids = ids.substring(1);
		return ids;
	}
	public static String toIdString(List<Integer> list) {
		String ids="";
		for(Integer id : list){						
			ids += ","+id;
		 }
		 ids = ids.substring(1);
		return ids;
	}
	public static List<Integer> toListIds(List<BeanModel> list) {
		List<Integer> retorno = new ArrayList<Integer>();		
		for(BeanModel item : list){			
			retorno.add(Integer.valueOf(item.get("id").toString()));
		 }
		return retorno;
	}
	
	public static void focoEnter(final Component comp, final Component comp2) {

		comp.addListener(Events.KeyDown, new KeyListener() {

			public void componentKeyDown(ComponentEvent event) {
				Integer c = event.getKeyCode();
				if (c == KeyCodes.KEY_ENTER)
					comp2.focus();

			}

		});

	}

	public static void Enter(Component A[]) {
		for (int i = 0; i < A.length - 1; i++)
			focoEnter(A[i], A[i + 1]);

	}
	
	public static String getCurrencySymbol(String code){			    
	    Map<String, String> currencyMap = currencyCodeMapConstants.currencyMap();	
		return currencyMap.get(code);
	}
	
}
